<template>
  <div class="app-container calendar-list-container">
    <!-- filter 条件查询 -->
    <div class="filter-container">
      <div style="display:inline-block;font-size:15px;">用户昵称：</div>
      <el-input
        clearable
        @keyup.enter.native="handleFilter"
        v-model="listQuery.nickName"
        style="width: 200px;margin-right:20px;margin-top:8px"
        class="filter-item"
        placeholder="请输入用户昵称"
      ></el-input>
      <div style="display:inline-block;font-size:15px;">设备编号：</div>
      <el-input
        clearable
        @keyup.enter.native="handleFilter"
        v-model="listQuery.equipmentNumber"
        style="width: 200px;margin-right:20px;margin-top:8px"
        class="filter-item"
        placeholder="请输入设备编号"
      ></el-input>
      <el-button
        class="filter-item"
        type="primary"
        v-waves
        icon="el-icon-search"
        @click="handleFilter"
        style="width: 80px;margin-right:20px;margin-top:8px"
      >查找</el-button>
      <el-button
          class="filter-item"
          type="primary"
          @click="handleDownload"
          icon="el-icon-download"
          :loading="downloadLoading"
          style="width: 120px;margin-right:20px;margin-top:8px"
        >批量导出列表</el-button>
    </div>
    <!--表格 -->
    <el-table
      :data="list"
      element-loading-text="给我一点时间"
      v-loading="loading.list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column prop="index" label="序号" align="center" width="80px"/>
      <el-table-column prop="nickName" label="用户昵称" align="center"/>>
      <el-table-column prop="userKind" label="用户类型" align="center">
        <template
          slot-scope="scope"
        >{{scope.row.userKind == 1?'普通用户':'后勤人员'}}</template>
      </el-table-column>
      <el-table-column prop="equipmentNumber" label="设备编号" align="center"/>
      <el-table-column label="设备电量" prop="cellVoltage" align="center">
        <template slot-scope="scope">{{scope.row.cellVoltage?scope.row.cellVoltage+'%':'0%'}}</template>
      </el-table-column>
      <el-table-column prop="lockState" label="操作状态" align="center">
        <template slot-scope="scope">{{scope.row.lockState == 1?'开锁':'关锁'}}</template>
      </el-table-column>
      <el-table-column prop="cTime" label="操作时间" align="center"/>>
    </el-table>
    <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.offset"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 回到顶部 -->
    <BackToTop></BackToTop>
  </div>
</template>
<script>
import { getLockrecordList } from "@/api/equipment";
import BackToTop from "@/components/BackToTop";
export default {
  name: "equipment_lock",
  components: {
    BackToTop
  },
  data() {
    return {
      loading: {
        list: false
      },
      dialog: {
        view: false
      },
      total: 0,
      listQuery: {
        offset: 1,
        limit: 10,
        equipmentNumber: "",
        nickName: ""
      },
      list: null,
      downloadLoading: false
    };
  },

  methods: {
    //导出
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = [
          "用户昵称",
          "用户类型",
          "设备编号",
          "设备电量",
          "操作状态",
          "操作时间"
        ];
        const filterVal = [
          "nickName",
          "userKind",
          "equipmentNumber",
          "cellVoltage",
          "lockState",
          "cTime"
        ];
        const data = this.formatJson(filterVal, this.list);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "开关锁列表"
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "cellVoltage") {
            if (v[j]) {
              return v[j] ? v[j] + "%" : "0%";
            }
          } else if (j === "userKind") {
            if (v[j]) {
              return v[j] === 1
                ? "普通用户"
                : "后勤人员";
            }
          } else if (j === "lockState") {
            if (v[j]) {
              return v[j] === 1 ? "开锁"  : "关锁";
            }
          } else {
            return v[j];
          }
        })
      );
    },
    //获取列表
    async getList() {
      this.loading.list = true;
      try {
        const { data } = await getLockrecordList({
          ...this.listQuery
        });
        this.list = data.records.map((item, index) => ({
          index: index + 1,
          ...item
        }));
        this.total = data.total;
      } catch (error) {}
      this.loading.list = false;
    },
    //点击查找
    handleFilter() {
      this.listQuery.offset = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.offset = val;
      this.getList();
    },
  },

  created() {
    this.getList();
  }
};
</script>
<style lang="scss" scoped>
/deep/ .el-carousel__container {
  height: 500px !important;
}
</style>
